package com.example.controller;

import com.example.anno.Log;
import com.example.mapper.EmpMapper;
import com.example.pojo.Dept;
import com.example.pojo.Emp;
import com.example.pojo.Result;
import com.example.service.DeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hyk~
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    //private static Logger log = LoggerFactory.getLogger(DeptController.class);
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)  //指定请求方式为GET
    @GetMapping
    public Result list() {
        log.info("查询全部部门数据");
        //调用Service查询部门数据
        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }
    @Log
    //根据id删除部门
    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id) { //@PathVariable 通过该注解来获取路径中id的值/{id}
        log.info("根据id删除部门数据：{}", id);
        //调用Service删除部门
        deptService.DeleteDept(id);
        return Result.success();
    }

    @Log
    //添加部门
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门数据{}", dept);
        deptService.addDept(dept);
        return Result.success();
    }

    //根据id查询部门
    @GetMapping("/{id}")
    public Result selectByDeptId(@PathVariable Integer id) {
        //日志记录
        log.info("根据id查询部门：{}", id);
        //调用service层功能
        Dept dept = deptService.selectByDeptId(id);
        //响应
        return Result.success(dept);
    }

    @Log
    //删除部门数据
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        //日志记录
        log.info("修改部门：{}", dept);
        //调用service层功能
        deptService.update(dept);
        //响应
        return Result.success();
    }
}
